/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ubbcluj.dbmanagementserver.service;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.ubbcluj.dbmanagementserver.model.DataMongo;
import com.ubbcluj.dbmanagementserver.model.Database;
import com.ubbcluj.dbmanagementserver.model.Table;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bson.types.ObjectId;
import sun.org.mozilla.javascript.internal.ObjArray;

/**
 *
 * @author Bea
 */
public class DatabaseService {

    private MongoClient mongo = null;

    public DatabaseService() {
        try {
            mongo = new MongoClient("localhost", 27017);
        } catch (UnknownHostException ex) {
            Logger.getLogger(DatabaseService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List<Database> readDBList() {
        return DBXMLSerializer.readDBFromXML();
    }

    public void addNewDataBase(String name) {
        mongo.getDB(name);
        List<Database> databases = readDBList();

        for (Database db : databases) {
            if (db.getName().equals(name)) {
                throw new IllegalArgumentException("Database with this name already exists");
            }
        }

        Database newDb = new Database(name, new ArrayList<Table>());
        databases.add(newDb);

        DBXMLSerializer.saveDBToXML(databases);
    }

    public void addTableToDB(String dbName, Table table) {
        DB mongodb = mongo.getDB(dbName);
        DBCollection mongoTable = mongodb.getCollection(table.getName());
        List<Database> databases = readDBList();

        for (Database db : databases) {
            if (db.getName().equals(dbName)) {
                for (Table t : db.getTableList()) {
                    if (t.getName().equals(table.getName())) {
                        throw new IllegalArgumentException("Table with this name already exists");
                    }
                }
                db.getTableList().add(table);
            }
        }
        DBXMLSerializer.saveDBToXML(databases);
    }

    public void deleteDb(String name) {
        DB mongodb = mongo.getDB(name);
        mongodb.dropDatabase();

        List<Database> databases = readDBList();

        for (Database db : databases) {
            if (db.getName().equals(name)) {
                databases.remove(db);
            }
        }
        DBXMLSerializer.saveDBToXML(databases);
    }

    public void deleteTable(String dbName, String name) {
        DB mongodb = mongo.getDB(dbName);
        DBCollection mongoTable = mongodb.getCollection(name);
        mongoTable.drop();
        List<Database> databases = readDBList();

        for (Database db : databases) {
            if (db.getName().equals(name)) {
                for (Table t : db.getTableList()) {
                    if (t.getName().equals(name)) {
                        db.getTableList().remove(t);
                    }
                }
            }
        }
        DBXMLSerializer.saveDBToXML(databases);
    }
    
    public void addDataToTable(String dbName, String tableName, String key, String value) {
        DB mongodb = mongo.getDB(dbName);
        DBCollection mongoTable = mongodb.getCollection(tableName);
        
        DBObject dbObject =  new BasicDBObject(key, value);
        mongoTable.insert(dbObject);
    }
    
    public void deleteDataFromTable(String dbName, String tableName, String key) {
        DB mongodb = mongo.getDB(dbName);
        DBCollection mongoTable = mongodb.getCollection(tableName);
        
        BasicDBObject searchQuery = new BasicDBObject();
	searchQuery.put("_id", key);
 
	mongoTable.remove(searchQuery);
    }

}
